import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;

class TrapezoidGUI {
    public static void showTrapezoidWindow() {
        JFrame frame = new JFrame("梯形计算器");
        frame.setSize(500, 400);
        frame.setLayout(new BorderLayout());

        // 输入面板
        JPanel inputPanel = new JPanel(new GridLayout(5, 2, 10, 10));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel topLabel = new JLabel("上底:");
        JTextField topField = new JTextField();
        JLabel bottomLabel = new JLabel("下底:");
        JTextField bottomField = new JTextField();
        JLabel heightLabel = new JLabel("高:");
        JTextField heightField = new JTextField();
        JLabel side1Label = new JLabel("腰1:");
        JTextField side1Field = new JTextField();
        JLabel side2Label = new JLabel("腰2:");
        JTextField side2Field = new JTextField();

        JButton calculateBtn = new JButton("计算");
        JButton drawBtn = new JButton("绘制图形");

        inputPanel.add(topLabel);
        inputPanel.add(topField);
        inputPanel.add(bottomLabel);
        inputPanel.add(bottomField);
        inputPanel.add(heightLabel);
        inputPanel.add(heightField);
        inputPanel.add(side1Label);
        inputPanel.add(side1Field);
        inputPanel.add(side2Label);
        inputPanel.add(side2Field);
        inputPanel.add(calculateBtn);
        inputPanel.add(drawBtn);

        // 结果面板
        JTextArea resultArea = new JTextArea();
        resultArea.setEditable(false);

        calculateBtn.addActionListener(e -> {
            try {
                double top = Double.parseDouble(topField.getText());
                double bottom = Double.parseDouble(bottomField.getText());
                double height = Double.parseDouble(heightField.getText());
                double side1 = Double.parseDouble(side1Field.getText());
                double side2 = Double.parseDouble(side2Field.getText());

                double area = (top + bottom) * height / 2;
                double perimeter = top + bottom + side1 + side2;

                String result = String.format("梯形计算结果:\n上底: %.2f\n下底: %.2f\n高: %.2f\n腰1: %.2f\n腰2: %.2f\n面积: %.2f\n周长: %.2f",
                        top, bottom, height, side1, side2, area, perimeter);
                resultArea.setText(result);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(frame, "请输入有效的数字！");
            }
        });

        drawBtn.addActionListener(e -> {
            try {
                double top = Double.parseDouble(topField.getText());
                double bottom = Double.parseDouble(bottomField.getText());
                double height = Double.parseDouble(heightField.getText());
                // 打开单独的绘图窗口
                new TrapezoidDrawingWindow(top, bottom, height);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(frame, "请输入有效的数字！");
            }
        });

        frame.add(inputPanel, BorderLayout.NORTH);
        frame.add(new JScrollPane(resultArea), BorderLayout.CENTER);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class TrapezoidDrawingWindow extends JFrame {
    private double top;
    private double bottom;
    private double height;

    public TrapezoidDrawingWindow(double top, double bottom, double height) {
        this.top = top;
        this.bottom = bottom;
        this.height = height;
        initializeUI();
    }

    private void initializeUI() {
        setTitle("梯形图形 - 上底: " + top + " 下底: " + bottom + " 高: " + height);
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        TrapezoidDrawingPanel drawingPanel = new TrapezoidDrawingPanel(top, bottom, height);
        add(drawingPanel);

        setVisible(true);
    }
}

class TrapezoidDrawingPanel extends JPanel {
    private double top;
    private double bottom;
    private double height;

    public TrapezoidDrawingPanel(double top, double bottom, double height) {
        this.top = top;
        this.bottom = bottom;
        this.height = height;
        setBackground(Color.WHITE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // 启用抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (top > 0 && bottom > 0 && height > 0) {
            g2d.setColor(Color.MAGENTA);
            g2d.setStroke(new BasicStroke(3));

            int centerX = getWidth() / 2;
            int centerY = getHeight() / 2;

            // 缩放梯形以便显示
            int scaledTop = (int) (top * 8);
            int scaledBottom = (int) (bottom * 8);
            int scaledHeight = (int) (height * 8);

            Path2D trapezoid = new Path2D.Double();
            trapezoid.moveTo(centerX - scaledBottom/2, centerY + scaledHeight/2); // 左下角
            trapezoid.lineTo(centerX + scaledBottom/2, centerY + scaledHeight/2); // 右下角
            trapezoid.lineTo(centerX + scaledTop/2, centerY - scaledHeight/2); // 右上角
            trapezoid.lineTo(centerX - scaledTop/2, centerY - scaledHeight/2); // 左上角
            trapezoid.closePath();

            g2d.draw(trapezoid);

            // 标注尺寸
            g2d.setColor(Color.RED);
            g2d.setFont(new Font("微软雅黑", Font.BOLD, 14));
            g2d.drawString("上底: " + top, centerX - scaledTop/2, centerY - scaledHeight/2 - 15);
            g2d.drawString("下底: " + bottom, centerX - scaledBottom/2, centerY + scaledHeight/2 + 25);
            g2d.drawString("高: " + height, centerX + scaledBottom/2 + 10, centerY);
        }
    }
}